package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-17 18:57
 */
public class Solution_0463 {
    /**
     * 岛屿的周长
     *
     * @param grid
     * @return
     */
    public int islandPerimeter(int[][] grid) {
        int size = 0;  //存周长

        int[][] bigGrid = new int[grid.length + 2][grid[0].length + 2];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                bigGrid[i+1][j+1]=grid[i][j];
            }

        }


        for (int i = 1; i <= grid.length; i++) {
            for (int j = 1; j <= grid[0].length; j++) {
                if (bigGrid[i][j]==1){  //是岛
                    int flag = 0;  //判断岛屿有几条边相邻
                    if (bigGrid[i + 1][j] == 1) {
                        flag++;
                    }
                    if (bigGrid[i][j + 1] == 1) {
                        flag++;
                    }
                    if (bigGrid[i - 1][j] == 1) {
                        flag++;
                    }
                    if (bigGrid[i][j - 1] == 1) {
                        flag++;
                    }
                    size = size + (4 - flag);
                }else {
                    continue;
                }


            }

        }


        return size;
    }

    public static void main(String[] args) {
        Solution_0463 solution_0463 = new Solution_0463();
        int[][] temp = {{1}};
        System.out.println(temp.length);
        System.out.println(temp[0].length);
              solution_0463.islandPerimeter(temp);
           System.out.println(solution_0463.islandPerimeter(temp));


    }
}
